package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.AttributeTemplate;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.AttributeTemplateService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AttributeTemplateServiceImpl implements AttributeTemplateService {
    @Autowired
    private AttributeTemplateMapper mapper;
    @Autowired
    private AttributeMapper attributeMapper;
    @Autowired
    private CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;

    @Override
    public void addNew(AttributeTemplateAddNewDTO atd) {
        log.debug(atd.toString());
        String name = atd.getName();
        int rows = mapper.countByName(name);
        if (rows > 0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加失败，属性模板名称被占用");
        }
        AttributeTemplate att = new AttributeTemplate();
        BeanUtils.copyProperties(atd,att);
        int insertRows = mapper.insert(att);
        if (insertRows != 1){
            String message = "添加属性模板失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        AttributeTemplateStandardVO currentVO = mapper.getStandardById(id);
        if (currentVO==null){
            String message = "删除失败，此属性模板不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }else if (attributeMapper.countByTemplateId(id)>0){
            String message = "删除属性模板失败，尝试删除的属性模板关联了某些属性！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }else if (categoryAttributeTemplateMapper.countByAttributeTemplate(id)>0){
            String message = "删除属性模板失败，尝试删除的属性模板关联了某些分类！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }else if (spuMapper.countByAttributeTemplate(id)>0){
            String message = "删除属性模板失败，尝试删除的属性模板关联了某些spu！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }else if (skuMapper.countByAttributeTemplateId(id)>0){
            String message = "删除属性模板失败，尝试删除的属性模板关联了某些sku！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int deleteRows = mapper.deleteById(id);
        if (deleteRows != 1){
            String message = "删除属性模板失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id, AttributeTemplateUpdateDTO updateDTO) {
        AttributeTemplateStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO==null){
            String message = "此属性模板已不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int count = mapper.countByNameAndNotId(updateDTO.getName(), id);
        if (count>0){
            String message = "修改属性模板失败，新的名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        AttributeTemplate attributeTemplate = new AttributeTemplate();
        attributeTemplate.setId(id);
        BeanUtils.copyProperties(updateDTO,attributeTemplate);
        int updateRows = mapper.update(attributeTemplate);
        if (updateRows!=1){
            String message = "修改属性模板失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public List<AttributeTemplateListItemVO> list() {
        List<AttributeTemplateListItemVO> list = mapper.list();
        return list;
    }

    @Override
    public AttributeTemplateStandardVO getStandardById(Long id) {
        AttributeTemplateStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO==null){
            String message = "查询属性模板详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardVO;
    }
}
